package cn.wangqi.personal.sms;

import cn.wangqi.personal.sms.util.SmsConfig;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * Created by admin on 2017/9/8.
 */
public class SmsUtil {
    public static void sendSms(String json, String phone) throws ClientException {
        IClientProfile profile = DefaultProfile.getProfile(SmsConfig.regionId, SmsConfig.accessKeyId, SmsConfig.accessKeySecret);
        DefaultProfile.addEndpoint(SmsConfig.regionId, SmsConfig.regionId, SmsConfig.product, SmsConfig.domain);
        IAcsClient client = new DefaultAcsClient(profile);

        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        //必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(SmsConfig.signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(SmsConfig.code);
        request.setTemplateParam(json);
        //可选-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        //request.setOutId("yourOutId");
        //请求失败这里会抛ClientException异常
        SendSmsResponse response = client.getAcsResponse(request);
        if (response.getCode() != null && "OK".equals(response.getCode())) {
            //请求成功
            System.out.println("短信接口返回的数据----------------");
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());
        }
    }
}
